import { createRouter, createWebHistory } from 'vue-router';
import useToken from '../stores/token';

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/home',
      meta: { title: '首页' }
    },
    {
      path: '/home',
      component: () => import('../pages/Home.vue'),
      name: 'home',
      meta: { title: '首页', name: 'home', isTab: true }
    },
    {
      path: '/category',
      component: () => import('../pages/Category.vue'),
      name: 'category',
      meta: { title: '分类', isTab: true, isShowNav: true }
    },
    {
      path: '/message',
      component: () => import('../pages/Message.vue'),
      name: 'message',
      meta: { title: '消息', isTab: true, isShowNav: true, requiresAuth: true } // 添加 requiresAuth 标记
    },
    {
      path: '/cart',
      component: () => import('../pages/Cart.vue'),
      name: 'cart',
      meta: { title: '购物车', isTab: true, isShowNav: true, isShowBack: true,requiresAuth: true  }
    },
    {
      path: '/user',
      component: () => import('../pages/User.vue'),
      name: 'user',
      meta: { title: '我的', isTab: true }
    },
    {
      path: '/login',
      component: () => import('../pages/Login.vue'),
      name: 'login',
      meta: { title: '登录', isTab: true, isShowNav: true, isShowBack: true }
    },
    {
      path: '/register',
      component: () => import('../pages/Register.vue'),
      name: 'register',
      meta: { title: '注册', isTab: true, isShowNav: true, isShowBack: true }
    },
    {
      path: '/goodslist/:category_id',
      component: () => import('../pages/GoodsList.vue'),
      props: true,
      name: 'goodslist',
      meta: { title: '商品列表', isTab: true, isShowNav: true, isShowBack: true }
    },
    {
      path: '/goodsDetail/:id',
      component: () => import('../pages/GoodsDetail.vue'),
      props: true,
      name: 'goodsDetail',
      meta: { title: '商品详情', isTab: false, isShowNav: true, isShowBack: true }
    },
    {
      path: '/order',
      component: () => import('../pages/Order.vue'),
      name: 'order',
      meta: { title: '订单确认', isTab: false, isShowNav: true, isShowBack: true }
    },
    {
      path: '/address/add',
      component: () => import('../pages/AddressAdd.vue'),
      name: 'addressAdd',
      meta: { title: '新增地址', isTab: false, isShowNav: true, isShowBack: true }
    },
    {
      path: '/address/list',
      component: () => import('../pages/AddressList.vue'),
      name: 'addressList',
      meta: { title: '地址管理', isTab: false, isShowNav: true, isShowBack: true }
    },
    {
      path: '/address/edit/:id',
      component: () => import('../pages/AddressList.vue'),
      name: 'addressEdit',
      props: true,
      meta: { title: '编辑地址', isTab: false, isShowNav: true, isShowBack: true }
    },
    {
      path: '/myddress',
      component: () => import('../pages/Myddress.vue'),
      name: 'myddress',
      meta: { title: '我的地址', isTab: true, isShowNav: true, isShowBack: true,requiresAuth: true }
    },
    {
      path: '/myorder',
      component: () => import('../pages/Myorder.vue'),
      name: 'myorder',
      meta: { title: '我的订单', isTab: true, isShowNav: true, isShowBack: true,requiresAuth: true }
    },
    // {
    //   path: '/pendingorders',
    //   component: () => import('../pages/pendingorders.vue'),
    //   name: 'pendingorders',
    //   meta: { title: '待付款', isTab: true, isShowNav: true, isShowBack: true,requiresAuth: true }
    // },
  ]
});

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title;
  if (title) {
    document.title = `${title} - 微商城`;
  }

  // 检查是否需要登录
  if (to.meta.requiresAuth) {
    const { token } = useToken();
    if (!token) {
      next({ name: 'login', query: { redirect: to.fullPath } }); // 重定向到登录页面，并带上重定向路径
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;